package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.BlindBoxService;
import com.lanchetech.bean.dto.BlindBoxPrizeDto;
import com.lanchetech.bean.request.BasePageReq;
import com.lanchetech.bean.request.BlindBoxPrizeReq;
import com.lanchetech.bean.vo.BlindBoxPrizeVo;
import com.lanchetech.bean.vo.BlindBoxVo;
import com.lanchetech.bean.request.BlindBoxReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.entity.BlindBox;
import com.lanchetech.entity.BlindBoxPrize;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(value = "盲盒管理接口", tags = "盲盒管理接口")
@RequestMapping("/api/blind/box")
public class BlindBoxController {

    @Autowired
    private BlindBoxService blindBoxService;

    @PostMapping("/editBlindBox")
    @OperatorLog("添加或编译盲盒")
    @ApiOperation("添加或编辑盲盒")
    @PreAuthorize("hasAuthority('product.blindbox.edit.auth')")
    public BaseResp editBlindBox(@RequestBody BlindBox blindBox) {
        BaseResp baseResp = new BaseResp();
        try {
            baseResp = blindBoxService.editBlindBox(blindBox);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @GetMapping("/getBlindBoxList")
    @ApiOperation("获取盲盒列表")
    public ResultData<BasePageResp<BlindBoxVo>> getBlindBoxList(@RequestBody BlindBoxReq blindBoxReq) {
        return blindBoxService.getBlindBoxList(blindBoxReq);
    }

    @PostMapping("/examine")
    @OperatorLog("盲盒审核")
    @ApiOperation("盲盒审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "盲盒ID", paramType = "query", dataType = "Long"), @ApiImplicitParam(name = "status", value = "审核状态", paramType = "query", dataType = "Byte")
    })
    @PreAuthorize("hasAuthority('product.blindbox.edit.auth')")
    public BaseResp examine(@RequestParam Long id, @RequestParam Byte status) {
        BaseResp baseResp = new BaseResp();
        try {
            blindBoxService.examine(id, status);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @PostMapping("/whether")
    @OperatorLog("盲盒开启/关闭")
    @ApiOperation("盲盒开启/关闭")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "盲盒ID", paramType = "query", dataType = "Long"), @ApiImplicitParam(name = "whether", value = "开启/关闭", paramType = "query", dataType = "Boolean")
    })
//    @PreAuthorize("hasAuthority('product.blindbox.edit.auth')")
    public BaseResp whether(@RequestParam Long id, @RequestParam Boolean whether) {
        BaseResp baseResp = new BaseResp();
        try {
            blindBoxService.whether(id, whether);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @PostMapping("/deleteBlindBoxById")
    @OperatorLog("盲盒开启/关闭")
    @ApiOperation("盲盒开启/关闭")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "盲盒ID", paramType = "query", dataType = "Long"), @ApiImplicitParam(name = "whether", value = "开启/关闭", paramType = "query", dataType = "Boolean")})
//    @PreAuthorize("hasAuthority('product.blindbox.edit.auth')")
    public BaseResp deleteBlindBoxById(@RequestParam Long id) {
        BaseResp baseResp = new BaseResp();
        try {
            blindBoxService.deleteBlindBoxById(id);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @PostMapping("/addPrize")
    @OperatorLog("添加或编辑奖项")
    @ApiOperation("添加或编辑奖项")
//    @PreAuthorize("hasAuthority('product.blindbox.edit.auth')")
    public BaseResp addPrize(@RequestBody BlindBoxPrize blindBoxPrize) {
        BaseResp baseResp = new BaseResp();
        try {
            blindBoxService.addPrize(blindBoxPrize);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @PostMapping("/getPrizes")
    @ApiOperation("奖项列表")
//    @PreAuthorize("hasAuthority('product.blindbox.edit.auth')")
    public ResultData<BasePageResp<BlindBoxPrizeVo>> getPrize(@RequestBody BlindBoxPrizeReq blindBoxPrizeReq) {
        return blindBoxService.getPrizes(blindBoxPrizeReq);
    }

    @PostMapping("/whetherOpenPrize")
    @ApiOperation("奖项是否开启/关闭")
    @OperatorLog("奖项是否开启/关闭")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prizeId", value = "奖项ID", paramType = "query", dataType = "Long")
    })
//    @PreAuthorize("hasAuthority('product.blindbox.edit.auth')")
    public BaseResp whetherOpenPrize(@RequestParam Long prizeId) {
        BaseResp baseResp = new BaseResp();
        try {
            blindBoxService.whetherOpenPrize(prizeId);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }


}
